package switchviz.graphic.gauge.graph2d.scale;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;

public class Graph2dBoardPanel extends PaddingPanel
{
	
	private static final long serialVersionUID = 2368874200523625113L;
	private Element _root = null;
	private Graph2dBoardDataModel _boardmodel = null;
	private Graph2dBoardPersistence _xmlhandler = null;
	private JMaskedTextField _txtsmallticks = null;
	private JMaskedTextField _txtbigticks = null;
	private JMaskedTextField _txtbigticklength = null;
	private JMaskedTextField _txtsmallticklength = null;
	private JMaskedTextField _txtbigtickwidth = null;
	private JMaskedTextField _txtsmalltickwidth = null;
	private JColorPanel _clscalepanel = null;
	private JColorPanel _cllabelpanel = null;

	public Element getElement()
	{
		getBoardModel().setNumbigticks(Integer.valueOf(_txtbigticks.getText()));
		getBoardModel().setNumsmallticks(Integer.valueOf(_txtsmallticks.getText()));
		getBoardModel().setLengthbigticks(Float.valueOf(_txtbigticklength.getText()));
		getBoardModel().setLengthsmallticks(Float.valueOf(_txtsmallticklength.getText()));
		getBoardModel().setWidthbigticks(Float.valueOf(_txtbigtickwidth.getText()));
		getBoardModel().setWidthsmallticks(Float.valueOf(_txtsmalltickwidth.getText()));
		getBoardModel().setScalecolor(_clscalepanel.getColor());
		getBoardModel().setLabelcolor(_cllabelpanel.getColor());
		_root = _xmlhandler.setDataModel(getBoardModel());
		return _root;
	}
	
	public Graph2dBoardPanel(Element root)
	{
		_root = root;
		initialize();
		setBoardModel(getXMLHandler().getDataModelFromElement());
	}
	
	private void initialize()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setName("Graph Board");
		Dimension dimlabelwidth = new Dimension(200, 20);
		_txtsmallticks = new JMaskedTextField("Number of Small Ticks: ", JMaskedTextField.INTNUM, dimlabelwidth);
		_txtbigticks = new JMaskedTextField("Number of Big Ticks: ", JMaskedTextField.INTNUM, dimlabelwidth);
		_txtbigticklength = new JMaskedTextField("Length Big Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtsmallticklength = new JMaskedTextField("Length Small Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtbigtickwidth = new JMaskedTextField("Width Big Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtsmalltickwidth = new JMaskedTextField("Width Small Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_clscalepanel = new JColorPanel("set Color for Scale: ", dimlabelwidth);
		_cllabelpanel = new JColorPanel("set Color for Labels: ", dimlabelwidth);
		
		this.add(_txtbigticks);
		this.add(_txtbigticklength);
		this.add(_txtbigtickwidth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtsmallticks);
		this.add(_txtsmallticklength);
		this.add(_txtsmalltickwidth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_clscalepanel);
		this.add(_cllabelpanel);
		//color.g
	}
	
	private Graph2dBoardDataModel getBoardModel()
	{
		if(_boardmodel == null)
			_boardmodel = new Graph2dBoardDataModel();
		return _boardmodel;
	}
	
	private Graph2dBoardPersistence getXMLHandler()
	{
		if (_xmlhandler == null)
			_xmlhandler = new Graph2dBoardPersistence(_root);
		return _xmlhandler;
	}
	
	private void setBoardModel(Graph2dBoardDataModel scalemodel)
	{
		_boardmodel = scalemodel;
		_txtsmallticks.setText(String.valueOf(_boardmodel.getNumsmallticks()));
		_txtbigticks.setText(String.valueOf(_boardmodel.getNumbigticks()));
		_txtbigticklength.setText(String.valueOf(_boardmodel.getLengthbigticks()));
		_txtsmallticklength.setText(String.valueOf(_boardmodel.getLengthsmallticks()));
		_txtbigtickwidth.setText(String.valueOf(_boardmodel.getWidthbigticks()));
		_txtsmalltickwidth.setText(String.valueOf(_boardmodel.getWidthsmallticks()));
		_clscalepanel.setColor(_boardmodel.getScalecolor());
		_cllabelpanel.setColor(_boardmodel.getLabelcolor());
	}
}
